﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using ObjectLibrary;
// -----------------------------------------------------------------------
// <copyright file="$safeitemrootname$.cs" company="$registeredorganization$">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------
namespace _0912089_0912172
{
    public class MyCombobox : FormItem
    {
        PlanarModel _panel;
        private List<PlanarModel> _lstItem;
        private int _itemSelectedIndex = 0;
        bool isExplained = false;

        public bool IsExplained
        {
            get { return isExplained; }
            set { isExplained = value; }
        }

        public int ItemSelectedIndex
        {
            get { return _itemSelectedIndex; }
            set {
                
                    _itemSelectedIndex = value; 
            }
        }



        public MyCombobox(String[] lstItem, float scale, Vector3 pos, float rotationY)
        {
            _itemType = 1;
            _lstItem = new List<PlanarModel>();

            int i=0;
            Vector2 TexSize = Vector2.Zero;
            foreach (String str in lstItem)
            {
                i++;
                Texture2D Textexture = TextureUntity.Create(_content, _graphicsDevice).ExportStringTexture(str);
                TexSize.X = Textexture.Width;
                TexSize.Y = Textexture.Height;
                PlanarModel pModel = new PlanarModel(_content, Textexture, 1, scale, pos, rotationY);
                _size = pModel.Size;
                pModel.Position = pModel.Position - new Vector3(0, (_size.Y) * i, 0);
                _lstItem.Add(pModel);
                    
            }
            Texture2D panel = TextureUntity.Create(_content, _graphicsDevice).createTexture((int)TexSize.X + 5, (int)TexSize.Y * i + 5, new Color(0, 0, 0, 230));
            _panel= new PlanarModel(_content, panel, 0, scale, pos , rotationY);
            _panel.Position = _panel.Position - new Vector3(0, (_size.Y / 2) * (i + 1), 0);

            Texture2D texture = TextureUntity.Create(_content, _graphicsDevice).ExportStringTexture(lstItem[0]);
            _model = new PlanarModel(_content, texture, scale, pos, rotationY);
            _size = ((PlanarModel)_model).Size;
            RotationY = rotationY;
            Position = pos;
            ItemSelectedIndex = 0;
        }
        private float t = 1f;
        float dLight = 0.02f;

        private void updateT()
        {
            if (t > 1)
            {
                dLight = -0.02f;
            }
            else if (t < 0.7)
            {
                dLight = 0.02f;
            }
            t += dLight;
        }
        public override void Update(GameTime gameTime)
        {
            if (isExplained)
            {
                updateT();
                for (int i = 0; i < _lstItem.Count; i++)
                {
                    if (i == _itemSelectedIndex)
                    {
                        _lstItem[i].FilterColor = Vector4.One * t;
                    }
                    else
                    {
                        _lstItem[i].FilterColor = Vector4.One;
                    }
                }
                ((PlanarModel)_model).FilterColor = new Vector4(0.5f,0.5f,0.5f,0.5f);
            }
            else
                base.Update(gameTime);
        }
        
        public override void Draw(GameTime gameTime, GraphicsDevice graphicsDevice, Effect effect, MyCamera camera)
        {
            _model.Draw(gameTime, graphicsDevice, effect, camera);
            if (isExplained)
            {
                _panel.Draw(gameTime, graphicsDevice, effect, camera);
                foreach (PlanarModel model in _lstItem)
                {
                    model.Draw(gameTime, graphicsDevice, effect, camera);
                }

            }
        }



        public Vector3 Position
        {
            get { return base.Position; }
            set
            {
                base.Position = value;
                int i = 0;
                foreach (PlanarModel model in _lstItem)
                {
                    i++;
                    model.Position = value - new Vector3(0, (model.Size.Y) * i, 0);
                }
                _panel.Position = value - new Vector3(0, (_size.Y / 2) * (i+1), 0); 
            }
        }

        public int GetItemAt(Vector3 contactPoint)
        {
            int i=0;
            foreach (PlanarModel model in _lstItem)
            {
                if(model.isClicked(contactPoint))
                {
                    _itemSelectedIndex = i;
                    return i;
                }
                i++;
            }
            if (isClicked(contactPoint))
                return _itemSelectedIndex;
            return -1;
        }
        public void SetItemAt(int index)
        {
            ((PlanarModel)_model).Texture = ((PlanarModel)_lstItem.ElementAt(index)).Texture;
        }

    }
}
